package cn.tedu.boot08.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@RestController
public class UploadController {
    @RequestMapping("/upload")
    public String upload(MultipartFile picFile) throws IOException {
        String fileName = picFile.getOriginalFilename();
        System.out.println(fileName);

        String suffix = fileName.substring(fileName.lastIndexOf("."));
        fileName = UUID.randomUUID()+suffix;
        System.out.println(fileName);

        String filePath = "d:/files";
        SimpleDateFormat sdf = new SimpleDateFormat("/yyyy/MM/dd/");
        String datePath = sdf.format(new Date());
        File fileDir = new File(filePath+datePath);
        if(!fileDir.exists()){
            fileDir.mkdirs();
        }

        picFile.transferTo(new File(filePath+datePath+fileName));

        return datePath+fileName;
    }

    @RequestMapping("/remove")
    public void remove(String url){
        System.out.println(url);
        new File("d:/files"+url).delete();
    }
}
